package com.auth.manager.web.sys;

import com.auth.manager.common.resp.Result;
import com.auth.manager.pojo.sys.UserInfo;
import com.auth.manager.pojo.sys.UserRoleRelation;
import com.auth.manager.service.sys.UserInfoService;
import com.auth.manager.service.sys.UserRoleRelationService;
import com.auth.manager.utils.page.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0.0
 * @Date: 2022/4/28 20:54
 * @Author ZhuYouBin
 * @Description 系统用户
 */
@RestController
@RequestMapping("/sys/account")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserRoleRelationService userRoleRelationService;

    @PostMapping("/create")
    public Result create(@RequestBody @Valid UserInfo userInfo) {
        userInfoService.createAccount(userInfo);
        return Result.success("新增成功");
    }

    @GetMapping("/delete/{uid}")
    public Result remove(@PathVariable String uid) {
        userInfoService.removeAccount(uid);
        return Result.success("删除成功");
    }

    @PostMapping("/update")
    public Result update(@RequestBody @Valid UserInfo userInfo) {
        userInfoService.updateAccount(userInfo);
        return Result.success("更新成功");
    }

    @GetMapping("/queryPage")
    public Result queryPage(UserInfo userInfo, PageVo pageVo) {
        PageVo queryPage = userInfoService.queryPage(userInfo, pageVo);
        return Result.success("查询成功").setData(queryPage);
    }

    @PostMapping("/assignRoleToUser")
    public Result assignRoleToUser(@RequestBody List<UserRoleRelation> list) {
        userRoleRelationService.assignRoleToUser(list);
        return Result.success("分配角色成功");
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestParam("token") String token) {
        Map<String, String> userInfo = userInfoService.getUserInfo(token);
        return Result.success("查询成功").setData(userInfo);
    }

    @GetMapping("/logout")
    public Result logout(@RequestParam("token") String token) {
        Map<String, String> userInfo = userInfoService.getUserInfo(token);
        return Result.success("注销成功");
    }

}
